<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class FlowsettingController extends Controller {
    
    private $flow_config;
    private $flow_flow_table;
    private $flow_process_table;
    private $flow_conclusion_table;
    private $flow_reason_table;
    public function __construct()
    {   
        $this->flow_config = Config::get('flow.flowsetting');
        $this->flow_flow_table = $this->flow_config['flow'];
        $this->flow_process_table = $this->flow_config['process'];
        $this->flow_conclusion_table = $this->flow_config['conclusion'];
        $this->flow_reason_table = $this->flow_config['reason'];
        $this->flow_track_table = $this->flow_config['track'];
        $this->flow_state_table = $this->flow_config['state'];
    }
    //流程设置-流程
    public function setFlows(&$request)
    {
        $input = $request->input();
        $data['flow_name'] = $input['flow_name'];
        $time = time();
        if( empty($data['flow_name']))
        {
            return $this->respond('empty_error');
        }
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        try {
            $id = DB::table($this->flow_flow_table)->insertGetId($data);
            return $this->respond('add_ok',['id'=>$id]);
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
    }
    //流程设置-步骤
    public function setProcess(&$request)
    {
        $input = $request->input();
        $data['flow_id'] = $input['flow_id'];
        $data['process_name'] = $input['process_name'];
        $data['order'] = $input['order'];
        $time = time();
        if(empty($data['flow_id']) || empty($data['process_name']) || empty($data['order']) )
        {
            return $this->respond('empty_error');
        }
        try {
            $flow = DB::table($this->flow_flow_table)->where('status',0)->find($data['flow_id']);
            if(empty($flow))
            {
                $return = ['code'=>500,'msg'=>'不存在的流程id！'];
                return response()->json($return);
            }
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        try {
            $id = DB::table($this->flow_process_table)->insertGetId($data);
            return $this->respond('add_ok',['id'=>$id]);
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
    }
    //流程设置-结果
    public function setConclusions(&$request)
    {
        $input = $request->input();
        $data['process_id'] = $input['process_id'];
        $data['conclusion_name'] = $input['conclusion_name'];
        $data['flag'] = $input['flag'];
        $data['next_process_id'] = isset($input['next_process_id']) ? $input['next_process_id'] : 0;
        $time = time();
        if(empty($data['process_id']) || empty($data['conclusion_name']) )
        {
            return $this->respond('empty_error');
        }
        try {
            $process = DB::table($this->flow_process_table)->where('status',0)->find($data['process_id']);
            if(empty($process))
            {
                $return = ['code'=>500,'msg'=>'不存在的步骤id！'];
                return response()->json($return);
            }
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        try {
            $id = DB::table($this->flow_conclusion_table)->insertGetId($data);
            return $this->respond('add_ok',['id'=>$id]);
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
    }
    //流程设置-原因
    public function setReasons(&$request)
    {
        $input = $request->input();
        $data['conclusion_id'] = $input['conclusion_id'];
        $data['reason_name'] = $input['reason_name'];
        $time = time();
        if(empty($data['conclusion_id']) || empty($data['reason_name'] ))
        {
            return $this->respond('empty_error');
        }
        try {
            $conclusion = DB::table($this->flow_conclusion_table)->where('status',0)->find($input['conclusion_id']);
            if(empty($conclusion))
            {
                $return = ['code'=>500,'msg'=>'不存在的结果id'];
                return response()->json($return);
            }
            if($conclusion->flag == 1)
            {
                $return = ['code'=>500,'msg'=>'成功结果不允许添加原因！'];
                return response()->json($return);
            }
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        try {
            $id = DB::table($this->flow_reason_table)->insertGetId($data);
            return $this->respond('add_ok',['id'=>$id]);
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
    }
    //流程设置-更新
    public function setUpdate($id, $type, $request)
    {   
        $input = $request->input();
        switch ($type) {
            case 1://更新流程表
                try {
                    $flow = DB::table($this->flow_flow_table)->where('status',0)->find($id);
                    if(empty($flow))
                    {
                        $return = ['code'=>500,'msg'=>'不存在的流程！'];
                        return response()->json($return);
                    }else
                    {   
                        DB::table($this->flow_flow_table)
                        ->where('id', $id)->update($input);
                        return $this->respond('update_ok');
                    }
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'错误参数传递！'];
                    return response()->json($return);
                }
            case 2://更新流程步骤表
                try {
                    $step = DB::table($this->flow_process_table)->where('status',0)->find($id);
                    if(empty($step))
                    {
                        $return = ['code'=>500,'msg'=>'不存在的步骤！'];
                        return response()->json($return);
                    }else
                    {
                        DB::table($this->flow_process_table)
                        ->where('id', $id)->update($input);
                        return $this->respond('update_ok');
                    }
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'错误参数传递！'];
                    return response()->json($return);
                }
            case 3://更新流程结果表
                try {
                    $flow = DB::table($this->flow_conclusion_table)->where('status',0)->find($id);
                    if(empty($flow))
                    {
                        $return = ['code'=>500,'msg'=>'不存在的结果！'];
                        return response()->json($return);
                    }else
                    {
                        DB::table($this->flow_conclusion_table)
                        ->where('id', $id)->update($input);
                        return $this->respond('update_ok');
                    }
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'错误参数传递！'];
                    return response()->json($return);
                }
            case 4://更新流程原因表
                try {
                    $flow = DB::table($this->flow_reason_table)->where('status',0)->find($id);
                    if(empty($flow))
                    {
                        $return = ['code'=>500,'msg'=>'不存在的结果！'];
                        return response()->json($return);
                    }else
                    {
                        DB::table($this->flow_reason_table)
                        ->where('id', $id)->update($input);
                        return $this->respond('update_ok');
                    }
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'错误参数传递！'];
                    return response()->json($return);
                }
            default:
                $return = ['code'=>500,'msg'=>'不存在的设置类型！'];
                return response()->json($return);
                break;
        }
    }
    //流程设置-查询
    public function setGet(&$request)
    {   
        $input = $request->input();
        $type = $input['type'];
        $id = isset($input['id']) ? $input['id'] : 0;
        switch ($type) {
            case 1://查询流程
                try {
                    $flow = DB::table($this->flow_flow_table)
                    ->where('status',0)->get();
                    $result['flow'] = $flow;
                    if(empty($flow))
                    {
                        $result['flow'] = [];
                    }
                    return $this->respond('get_ok', $result);
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
                    return response()->json($return);
                }
            case 2://查询流程下步骤
                try {
                    $flow = DB::table($this->flow_process_table)
                    ->where('status',0)
                    ->where('flow_id',$id)
                    ->orderBy('order','asc')->get();
                    $result['flow'] = $flow;
                    if(empty($flow))
                    {
                        $result['flow'] = [];
                    }
                    return $this->respond('get_ok', $result);
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
                    return response()->json($return);
                }
            case 3://查询步骤下结果
                try {
                    $flow = DB::table($this->flow_conclusion_table)
                    ->where('status',0)
                    ->where('process_id',$id)->get();
                    $result['flow'] = $flow;
                    if(empty($flow))
                    {
                        $result['flow'] = [];
                    }
                    return $this->respond('get_ok', $result);
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
                    return response()->json($return);
                }
            case 4://查询结果下原因
                try {
                    $flow = DB::table($this->flow_reason_table)
                    ->where('status',0)
                    ->where('conclusion_id',$id)->get();
                    $result['flow'] = $flow;
                    if(empty($flow))
                    {
                        $result['flow'] = [];
                    }
                    return $this->respond('get_ok', $result);
                }catch (\Exception $e) {
                    $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
                    return response()->json($return);
                }
            default:
                $return = ['code'=>500,'msg'=>'不存在的设置类型！'];
                return response()->json($return);
                break;
        }
    }
    //启动流程
    public function setState(&$request)
    {
        $input = $request->input();
        $data['flow_id'] = $input['flow_id'];
        $data['process_id'] = $input['process_id'];
        $data['conclusion_id'] = $input['conclusion_id'];
        $data['reason_id'] = isset($input['reason_id']) ? $input['reason_id'] : 0;
        $data['status'] = $input['status'];
        $time = time();
        if( empty($data['flow_id']) || empty($data['process_id']) || empty($data['conclusion_id']) )
        {
            return $this->respond('empty_error');
        }
        $flow = DB::table($this->flow_flow_table)->where('status',0)->find($data['flow_id']);
        if(empty($flow))
        {
            $return = ['code'=>500,'msg'=>'不存在的流程！'];
            return response()->json($return);
        }
        $process = DB::table($this->flow_process_table)->where('status',0)->find($data['process_id']);
        if(empty($process))
        {
            $return = ['code'=>500,'msg'=>'不存在的步骤！'];
            return response()->json($return);
        }
        $conclusion = DB::table($this->flow_conclusion_table)->where('status',0)->find($data['conclusion_id']);
        if(empty($conclusion))
        {
            $return = ['code'=>500,'msg'=>'不存在的结果！'];
            return response()->json($return);
        }
        if($data['reason_id'])
        {
            $reason = DB::table($this->flow_reason_table)->where('status',0)->find($data['reason_id']);
            if(empty($reason))
            {
                $return = ['code'=>500,'msg'=>'不存在的原因！'];
                return response()->json($return);
            }
        }
        $res = DB::table($this->flow_track_table)->where([
            ['flow_id', '=', $data['flow_id']],
            ['process_id', '=', $data['process_id']],
            ['status', '=', '1'],
            ])->first();
        if(!empty($res))
        {
            $return = ['code'=>500,'msg'=>'该流程已经结束！'];
            return response()->json($return);
        }
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        $data['next_process_id'] = $conclusion->next_process_id;
        try {
            DB::table($this->flow_state_table)->delete();
            DB::table($this->flow_state_table)->insertGetId($data);
            DB::table($this->flow_track_table)->insertGetId($data);
            return $this->respond('add_ok',['next_process_id'=>$conclusion->next_process_id]);
        }catch (\Exception $e) {
            $return = ['code'=>500,'msg'=>'不存在的表或表结构'];
            return response()->json($return);
        }
    }
}

?>
